<?php

namespace Apps\AppApi\Method\BeautyParlor;
use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\BeautyParlorInteractive;
use Apps\Common\Models\UserBase;
use PhalconPlus\Api\Common\ResultStatus;

 /**
  * 美粒互动显示列表
  * @author      545
  * @date        2016年4月19日
  * @version     2.0.2
  * @copyright   Copyright 2016 meelier.com
  */
class BpInteractiveList extends BaseMethod
{
    /**
     * @var \Apps\Common\Models\BeautyParlorComment array
     */
    private $data = [];

    public function run()
    {
        $bp_id = intval($this->getDataItem('bp_id',0));
        $page = intval($this->getDataItem('page',1));

        if ($bp_id==0){
            return $this->errorLog(ResultStatus::URL_PARAM_CANNOT_EMPTY, '参数异常');
        }
        $limit = $this->getConfig()->limit;
        $offset = ($page-1)*$limit;

        $interactive = BeautyParlorInteractive::query()
                            ->columns(['id,content,pics,user_id,addtime'])
                            ->where("bp_id = {$bp_id} AND state = 1")
                            ->orderBy('addtime desc')
                            ->limit($limit,$offset)
                            ->execute()->toArray();
            
            //处理图片
            foreach ($interactive as $k => $val) {
            
                $InteractiveLists[$k]['content'] = unserialize(base64_decode($val['content'])) ?: $val['content'];
                if (json_decode($val['pics'], true)) {
                    $question_pics = PicUrl::Question(json_decode($val['pics'], true), $this->getDi());
                    $interactive[$k]['pics'] = $question_pics;
                }else {
                    $interactive[$k]['pics'] = null;
                }
            
            }
            //设置互动值
            $this->data['bp_interactives'] = $interactive;
            $bossid = $interactive[0]['user_id'];
            //查询美容院老板账号信息
            $bossinfo = UserBase::query()
                        ->columns(['user_id,user_nickname,user_gender,user_cover,persional_sign'])
                        ->where("user_id = :bid:")
                        ->bind(['bid'=>$bossid])
                        ->execute()->getFirst();
            if ($bossinfo){
                $bossinfo=$bossinfo->toArray();
                $bossinfo['user_cover'] = PicUrl::UserCover($bossinfo['user_cover'], $this->getDi());
                $bossinfo['persional_sign'] = $bossinfo['persional_sign'] ? unserialize(base64_decode($bossinfo['persional_sign'])) : '';
            }
            else {
                $bossinfo=null;
            }
            $this->data['bp_bossinfo'] = $bossinfo;
        $this->setResult($this->data);
    }

}